package com.hemj.fileInfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hemj.common.core.domain.R;
import com.hemj.common.core.domain.entity.SysDept;
import com.hemj.fileInfo.domain.FileRole;
import com.hemj.fileInfo.service.impl.FileRoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.*;

import com.github.pagehelper.Page;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.hemj.common.annotation.Log;
import com.hemj.common.core.controller.BaseController;
import com.hemj.common.core.domain.AjaxResult;
import com.hemj.common.enums.BusinessType;
import com.hemj.fileInfo.domain.FileInfo;
import com.hemj.fileInfo.vo.FileInfoVo;
import com.hemj.fileInfo.bo.FileInfoBo;
import com.hemj.fileInfo.service.impl.FileInfoServiceImpl;
import com.hemj.common.utils.poi.ExcelUtil;
import com.hemj.common.core.page.TableDataInfo;

/**
 * 文件Controller
 * 
 * @author hemj
 * @date 2024-09-13
 */
@Api(tags = "文件接口")
@RestController
@RequestMapping("/fileInfo/fileInfo")
public class FileInfoController extends BaseController
{
    @Autowired
    private FileInfoServiceImpl fileInfoService;
    @Autowired
    private FileRoleServiceImpl fileRoleService;

    /**
     * 分页查询文件
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:list')")
    @PostMapping("/list")
    @ApiOperation("分页查询文件")
    public R<List<FileInfoBo>> list(@RequestBody FileInfoVo fileInfo)
    {
        Page page = startPage();
        List<FileInfoBo> list = fileInfoService.queryList(fileInfo);
        return getResDataTable(list,page);
    }

    @PostMapping("/hotList")
    @ApiOperation("分页查询文件")
    public R<List<FileInfoBo>> hotList(@RequestBody FileInfoVo fileInfo)
    {
        fileInfo.setNotType("folder");
        Page page = startPage();
        List<FileInfoBo> list = fileInfoService.hotList(fileInfo);
        return getResDataTable(list,page);
    }

    @PostMapping("/myHotList")
    @ApiOperation("分页查询文件")
    public R<List<FileInfoBo>> myHotList(@RequestBody FileInfoVo fileInfo)
    {
        Set<Long> fileRoles = fileRoleService.getMyFileIds();
        if (ObjectUtils.isEmpty(fileRoles)) {
            fileRoles.add(-1l);
        }
        fileInfo.setFileIds(fileRoles);
        fileInfo.setNotType("folder");
        Page page = startPage();
        List<FileInfoBo> list = fileInfoService.hotList(fileInfo);
        return getResDataTable(list,page);
    }

    /**
     * 获取部门下拉树列表
     */
    @PostMapping("/treeselect")
    public AjaxResult treeselect(@RequestBody FileInfoVo fileInfo)
    {
        List<FileInfoBo> fileInfoBos = fileInfoService.queryList(fileInfo);
        return AjaxResult.success(fileInfoService.buildFolderTreeSelect(fileInfoBos));
    }

    /**
     * 获取部门下拉树列表
     */
    @PostMapping("/myTreeSelect")
    public AjaxResult myTreeSelect(@RequestBody FileInfoVo fileInfo)
    {
        Set<Long> fileRoles = fileRoleService.getMyFileIds();
        if (ObjectUtils.isEmpty(fileRoles)) {
            fileRoles.add(-1l);
        }
        fileInfo.setFileIds(fileRoles);
        List<FileInfoBo> fileInfoBos = fileInfoService.queryList(fileInfo);
        return AjaxResult.success(fileInfoService.buildFolderTreeSelect(fileInfoBos));
    }

    /**
     * 查询文件列表
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:list')")
    @PostMapping("/myQuery")
    @ApiOperation("查询文件列表")
    public R<List<FileInfoBo>> myQuery(@RequestBody FileInfoVo fileInfo)
    {
        Set<Long> fileRoles = fileRoleService.getMyFileIds();
        if (ObjectUtils.isEmpty(fileRoles)) {
            fileRoles.add(-1l);
        }
        fileInfo.setFileIds(fileRoles);
        List<FileInfoBo> list = fileInfoService.queryList(fileInfo);
        return getResDataTable(list);
    }

    /**
     * 查询文件列表
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:list')")
    @PostMapping("/query")
    @ApiOperation("查询文件列表")
    public R<List<FileInfoBo>> query(@RequestBody FileInfoVo fileInfo)
    {
        List<FileInfoBo> list = fileInfoService.queryList(fileInfo);
        return getResDataTable(list);
    }

    /**
     * 导出文件
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    //@ApiOperation("导出文件")
    public void export(HttpServletResponse response, FileInfoVo fileInfo)
    {
        List<FileInfoBo> list = fileInfoService.queryList(fileInfo);
        ExcelUtil<FileInfoBo> util = new ExcelUtil<FileInfoBo>(FileInfoBo.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:query')")
    @PostMapping(value = "/get")
    @ApiOperation("获取文件详细信息")
    public R<FileInfoBo> getInfo(@RequestBody FileInfoVo fileInfo)
    {
        return R.ok(fileInfoService.getOneById(fileInfo.getId()));
    }

    @PostMapping(value = "/setView")
    @ApiOperation("获取文件详细信息")
    public R setView(@RequestBody FileInfoVo fileInfo)
    {
        fileInfoService.setView(fileInfo.getId());
        return R.ok();
    }

    /**
     * 新增文件
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @ApiOperation("新增文件")
    public R add(@RequestBody FileInfoVo fileInfo)
    {
        fileInfo.setCreateBy(getNickName());
        fileInfo.setCreateTime(new Date());
        return R.ok(fileInfoService.saveCascade(fileInfo));
    }

    /**
     * 修改文件
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateFolder")
    @ApiOperation("修改增文件")
    public R updateFolder(@RequestBody FileInfoVo fileInfo)
    {
        fileInfo.setUpdateBy(getNickName());
        fileInfo.setUpdateTime(new Date());
        return R.ok(fileInfoService.updateCascade(fileInfo));
    }


    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/addFolder")
    @ApiOperation("修改增文件")
    public R addFolder(@RequestBody FileInfoVo fileInfo)
    {
        return R.ok(fileInfoService.addFolder(fileInfo));
    }


    /**
     * 删除文件
     */
    //@PreAuthorize("@ss.hasPermi('fileInfo:fileInfo:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
	@PostMapping("/delete/{ids}")
    @ApiOperation("删除文件")
    public R remove(@PathVariable Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(FileInfo::getAncestors,id);
            fileInfoService.remove(wrapper);
        }
        fileInfoService.removeByIds(Arrays.asList(ids));
        return R.ok("ok");
    }
}
